package controllers;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import play.libs.Json;
import play.mvc.*;

public class MyTable extends Controller {
  	
  	public static Result getModel(String modelName) {
  		String user = session("name");
  		
  		if (!AuthorHelper.canViewTable(user, modelName)) {
  			return internalServerError("无权访问");
  		}

  		Class c = null;
  		try {
			c = Class.forName(modelName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  		Method m = null;
		try {
			m = c.getMethod("create", String.class);		
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  		try {
  			return ok(Json.toJson(m.invoke(c, user)));
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  		
  		return internalServerError("not support");
  		/*
  		if (modelName.equalsIgnoreCase("insurance")) {
  			TableModel m = InsuranceTable.create();
  			return ok(Json.toJson(m));
  		} else if (modelName.equalsIgnoreCase("repair")) {
  			TableModel m = RepairRequestTable.create();
  			return ok(Json.toJson(m));
  		} else if (modelName.equalsIgnoreCase("repairrequest")) {
  			TableModel m = RepairTable.create();
  			return ok(Json.toJson(m));
  		} 
  		else {
  			return internalServerError("not support");
  		}*/
  	}
}

